;(async function () {
  const db = require('./db/connectdb')
  const express = require('express')
  const formidableMiddleware = require('express-formidable')

  await db
  console.log('数据库连接成功')
  const app = express()
  const adminRouter = require('./routers/adminRouter')
  const loginRouter = require('./routers/loginRouter')
  const advRouter = require('./routers/advRouter')

  // 处理post请求上传的参数

  //   app.use(express.urlencoded({ extended: true }))
  //   app.use(express.json())
  app.use(express.static('./public'))
  app.use(
    formidableMiddleware({
      uploadDir: './public/uploadDir',
      keepExtensions: true,
    })
  )
  // 利用cors.处理跨域
  // 使用路由器中间件
  app.use(adminRouter)
  app.use(loginRouter)
  app.use(advRouter)

  app.listen(5000, (err) => {
    if (err) console.log('服务器启动失败', err)
    else console.log('服务器启动成功')
  })
})()
